<?php
require_once "../class/fenajax.class.php";
include "../dyn/connexion.php";

$f = newFen(__FILE__,"Le Cadavre Exquis",2);

$f->displayHeader();
echo "
<script type=\"text/javascript\">
	menuAccordionAuto();
	$(function() {
		$( \"#datepicker\" ).datepicker();
	});
	 $(function() {
		$( \"#FinCadavre\" ).dialog({
		  autoOpen: false,
		  resizable: false,
		  modal: true,
		  width: 450,
		  buttons: {
			\"Finaliser\": function() {
				cadavreExquisDelete();
				$( this ).dialog( \"close\" );
			},
			\"Annuler\": function() {
				$( this ).dialog( \"close\" );
			}
		  }		  
		});
		
		$( \"#finir_cadavre\" ).click(function() {
		  $( \"#FinCadavre\" ).dialog( \"open\" );
		});
		
		$( \"#DelCadavre\" ).dialog({
		  autoOpen: false,
		  resizable: false,
		  modal: true,
		  width: 450,
		  buttons: {
			\"Suprimer\": function() {
				cadavreExquisDelete();
				$( this ).dialog( \"close\" );
			},
			\"Annuler\": function() {
				$( this ).dialog( \"close\" );
			}
		  }		  
		});
		
		$( \"#supp_cadavre\" ).click(function() {
		  $( \"#DelCadavre\" ).dialog( \"open\" );
		});
	});
</script>";
?>

<?php
if (isset($_SESSION['statut'])) {	
	if ($_SESSION['statut'] >= 2) {		
		$sql = mysql_query("SELECT * FROM CadavreExquis WHERE statut=1 ORDER BY id DESC LIMIT 1");
		$num_rows = mysql_num_rows($sql);		
		if($num_rows != NULL){
			$sql2 = mysql_query("SELECT id,idcadavre,iduser,contenu FROM CadavreExquis WHERE statut=1 ORDER BY id DESC LIMIT 1");
			$row=mysql_fetch_array($sql2);
			$idlast=$row['id'];
			$idlastuser=$row['iduser'];
			$idcadavre=$row['idcadavre'];
			$lastcontenu=$row['contenu'];
			echo "<div class=\"center\"><h2>Derniere ligne du cadavre : <strong>".$lastcontenu." </strong></h2></div>";
			$iduser=$_SESSION['user_id'];
			$login=$_SESSION['login'];
			if( $idlastuser == $iduser ){
				echo '<div class="center"><h2>Vous ne pouvez continuez le jeu seul</h2></div>';
			
			}else{				
				echo "<div id=\"erreur\" class=\"center message ui-widget ui-corner-all ui-state-error\" style=\"display: none\">
							<span class=\"ui-icon ui-icon-alert\"></span>
							<span class=\"text\" id=\"erreur\">Vous devez saisir quelquechose !</span>
						</div><br /><br />
				";
				echo '<form id="form">
					<div class="center">			
					<input class="ui-corner-all" type="texte" id="cadavre">
					<input id="idCadavre" type="hidden" name="Cadavre" value="'.$idcadavre.'" />
					<input id="iduser" type="hidden" name="iduser" value="'.$iduser.'" />
					<input id="idlast" type="hidden" name="iduser" value="'.$idlast.'" />
					<input id="auteur" type="hidden" name="auteur" value="'.$login.'" />
					<input  type=button value="Envoyer" onclick="cadavreExquisSend()" class="ui-state-default ui-corner-all button" >
					</div>
				</form>
				<br /><br />
				';
			}
		}else{
			$sql1 = mysql_query("SELECT * FROM CadavreExquis WHERE statut=0 ORDER BY id DESC LIMIT 1");
			$num_row = mysql_num_rows($sql1);		
			if($num_row != NULL){
				$r=mysql_fetch_array($sql1);
				$idlast=$r['id'];
				$dt=strtotime($r['date']);
				$idcadavre=$r['idcadavre'];
				$iduser=$_SESSION['user_id'];
				$login=$_SESSION['login'];
				if($dt<=time()){
					echo "<div class=\"center\"><h2>Vous étes le premier a jouer </h2></div>";
					
					/* echo '<p><span id="erreur" style="border:3px solid silver; color:red; display:none; width:70%;">Vous devez saisir quelquechose !</span></p>'; */
					
					echo "<div id=\"erreur\" class=\"center message ui-widget ui-corner-all ui-state-error\" style=\"display: none\">
							<span class=\"ui-icon ui-icon-alert\"></span>
							<span class=\"text\" id=\"erreur\">Vous devez saisir quelquechose !</span>
						</div><br /><br />
					";
					echo '<form id="form">	
							<div class="center">		
							<input class="ui-corner-all" type="texte" id="cadavre">
							<input id="idCadavre" type="hidden" name="Cadavre" value="'.$idcadavre.'" />
							<input id="idlast" type="hidden" name="iduser" value="'.$idlast.'" />
							<input id="iduser" type="hidden" name="iduser" value="'.$iduser.'" />
							<input id="auteur" type="hidden" name="auteur" value="'.$login.'" />
							<input  type=button value="Envoyer" onclick="cadavreExquisSend()" class="ui-state-default ui-corner-all button">
							</div>
						</form>
						<br /><br />
						';			
				}else{
					$dateCE=strftime('%A %d %B',$dt);
					echo "<div class=\"center\">
						".BMInfo("Note: Le prochain cadavre exquis commence le ".$dateCE."",false)."<br /><br /><br />
					</div>
				";		
				}
			}else{
				echo "<div class=\"center\">
						".BMInfo("Note: Il n'y pas de cadavre exquis en cours ou prévu",false)."<br /><br /><br />
					</div>
				";
			}
		
		}
	}
	
	
	echo "
	<div id=\"accordionAuto\">";
		if ($_SESSION['statut'] >= 3) {
			echo "<div class=\"group\">
					<h3>Gestion des Cadavres Exquis</h3>
						<div class=\"center\">";
							$sql4 = mysql_query("SELECT * FROM CadavreExquis WHERE statut!=2");
							$num_rows = mysql_num_rows($sql4);		
							if($num_rows != NULL){
								$sql3 = mysql_query("SELECT idcadavre,iduser,contenu FROM CadavreExquis WHERE statut!=2 ORDER BY id DESC LIMIT 1");
								$row=mysql_fetch_array($sql3);
								$sql5 = mysql_query("SELECT idcadavre,iduser,contenu FROM CadavreExquis WHERE statut=1 ORDER BY id DESC LIMIT 1");
								$num_rowssql5 = mysql_num_rows($sql5);
								$idcadavre=$row['idcadavre'];
								echo '<form id="form"">
										<input id="idCadavre" type="hidden" name="Cadavre" value="'.$idcadavre.'" />';
										if($num_rowssql5 != NULL){
											echo '<br /><br />
												<div class="center">
												<div id="FinCadavre" class="center" title="Finaliser le cadavre exquis">
														'.BMInfo("Voulez vous vraiment finaliser le cadavre ?",false).'</p>
												</div>
												<input id="finir_cadavre" type=button value="Finaliser le cadavre exquis" class="ui-state-default ui-corner-all button" >
												</div>
												';
												
										}
										echo '<br /><br />
										<div id="DelCadavre" class="center" title="Suprimer le cadavre exquis">
														'.BMInfo("Voulez vous vraiment suprimer le cadavre ?",false).'
										</div>
										<input id="supp_cadavre" type=button value="Supprimer le cadavre exquis" class="ui-state-default ui-corner-all button" ><br /><br /><br />
									</form>';
							}else{							
								$creatsql =mysql_query("SELECT idcadavre FROM CadavreExquis where statut =2 ORDER BY id DESC LIMIT 1");
								$creatsqlnb=mysql_num_rows($creatsql);
								if($creatsqlnb != NULL){
									$rowing=mysql_fetch_array($creatsql);
									$idcadavre = $rowing['idcadavre'] + 1;
								}else{
									$idcadavre = 0;
								}	
									/* echo '<p><span id="erreurdate" style="border:3px solid silver; color:red; display:none; width:70%;">Vous devez saisir une date</span></p>'; */
									echo "<div id=\"erreurdate\" class=\"message ui-widget ui-corner-all ui-state-error\" style=\"display: none\">
											<span class=\"ui-icon ui-icon-alert\"></span>
											<span class=\"text\" id=\"erreur\">Vous devez renseigner une date</span>
										</div><br /><br />
									";
									echo '<form id="form">
											<input id="idCadavre" type="hidden" name="Cadavre" value="'.$idcadavre.'" />
											<input  type=button value="Creer un nouveau cadavre exquis" class="ui-state-default ui-corner-all button" onclick="cadavreExquisCreate()" /><br /><br /><br />
											<label for="datepicker">Date de début:</label>
											<input class="ui-corner-all" type="text" id="datepicker" />
											<br /><br />
										</form>';							
							}
		
		
		echo 			'</div>
				</div>';
				
		}
		
		
		
		echo "<div class=\"group\">
			<h3>Derniers Cadavres Exquis</h3>
			<div class=\"center\"><br />";
				$sql ="SELECT * FROM CadavreExquis where statut =2 ORDER BY date DESC";
				$requete=mysql_query($sql);
				while($row=mysql_fetch_array($requete)){
					$id = $row['id'];
					$auteur=$row['auteur'];
					$contenu=$row['contenu'];
					$date=strftime('%d/%m/%Y %H:%M:%S', strtotime($row['date']));
					
					echo "<div class=\"div_cadavre ui-widget ui-corner-all ui-widget-content\" >
					";
							
					
					if ($_SESSION['statut'] >= 3) {
					
						echo "<div class=\"suppCadavre ui-widget ui-corner-all ui-state-default ui-state-valid\">
								<a href=\"#\" class=\"ui-icon ui-icon-trash\" id=\"".$id."\" onclick=\"suppCadavre(event)\"></a>
							</div>
						";
					}
							
					echo "	<div class=\"center\">
								<h3>Par ".$auteur."</h3>
								<p>".$contenu."</p>
								<br />
								<p class=\"date\" >".$date."</p>
							</div>
						</div>
					";
				}		
	  echo '</div>
		</div>';
				
echo '</div>';
	
}
$f->displayFooter();

?>










